﻿
//创建xml对象函数
function createXmlHttpRrquest()	{
	var xmlHttp=null;
	try{
		xmlHttp=new XMLHttpRequest();
	}catch (e){
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
 }
	return xmlHttp;
}
//var cr_album = document.getElementById("cr_album"); 
//cr_album.onclick = createAlbum; 
function createAlbum() {
	var cr_album = document.getElementById("cr_album");
	document.getElementById("album").innerHTML = "<form id=albumForm action=\"#\" method=\"post\">"
											+"标题：<input type=\"text\" size=\"30\" name=\"title\" id=\"title\"/><br />"
											+"标签：<input type=\"text\" size=\"30\" name=\"tag\" id=\"tag\"/><br />"
											+"描述<textarea name=\"description\" id=\"description\"  ></textarea><br />"
											+"设置权限：<input type=\"radio\" name=\"privilege\" value=\"公开\" checked/>公开"
											+"<input type=\"radio\" name=\"privilege\" value=\"好友查看\" />好友查看"
											+"<input type=\"radio\" name=\"privilege\" value=\"群组查看\" />群组查看<br />"
											+"<input type=\"button\" id=\save_album\" value=\"保存\" onclick=\"saveAlbum()\"/>"
											+"<input type=\"button\" id=\"cancel_album\" value=\"取消\" onclick=\"cancelAlbum()\"/>"
											+"</form>";
}
var xmlHttp = createXmlHttpRrquest(); 
function saveAlbum() {
	//创建xml对象
	var albumForm = document.forms["albumForm"]; 
	var title = albumForm["title"].value; 
	var tag = albumForm["tag"].value;
	var privilege = albumForm["privilege"];
	for(var i = 0;i < privilege.length;i++) {
		if(privilege[i].checked == true) {
			 priv = privilege[i].value; 
		}
	} 
	var description = albumForm["description"].value;
	var queryString = "title=" + title + "&tag=" + tag + "&description=" + description + "&privilege=" + priv ;
	var url = "photo/createAlbum";
	xmlHttp.onreadystatechange = callbackAlbum; //回调函数
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
	xmlHttp.send(queryString);
}
//回调函数callbackAlbum
function callbackAlbum() {
	if (xmlHttp.readyState == 4){ 
		if(xmlHttp.status == 200) {
			albumList();
		} else {
			document.getElementById("al").innerHTML = "<p>failed</p>";
		}
	} 
}
function albumList() {
	document.getElementById("album_name").innerHTML = xmlHttp.responseText;
	//document.getElementById("album_id").innerHTML = xmlHttp.responseText;
	cancelAlbum();
}
					
function cancelAlbum() {
	var album = document.getElementById("album");
	var albumForm = document.forms["albumForm"];	
	album.removeChild(albumForm);	
}

function uploadAgain() {
	//document.getElementById("againup").innerHTML = "<input type=\"file\" name=\"image\"/>";
	var againup = document.getElementById("againup");
	var input = document.createElement("input");
	input.setAttribute("type","file");
	input.setAttribute("id","upinput")
	input.setAttribute("name","image[]");
	againup.appendChild(input);
	var cancel = document.createElement("input");
	cancel.setAttribute("type","button");
	cancel.setAttribute("id","cancel");
	cancel.setAttribute("onclick","cancelup()");
	cancel.setAttribute("value","取消");
	againup.appendChild(cancel);
	var br = document.createElement("br");
	br.setAttribute("id","upbr");
	againup.appendChild(br);
}
function cancelup() {
	var againup = document.getElementById("againup");
	var upinput = document.getElementById("upinput");
	var cancel = document.getElementById("cancel");
	var upbr = document.getElementById("upbr");
	againup.removeChild(upinput);
	againup.removeChild(cancel);
	againup.removeChild(upbr);
}
//添加描述表单
function createDescForm() {
	document.getElementById("oneDesc").innerHTML ="<p>单个上传</p>"
													+"<p>标签<input type=\"text\" size=\"30\" name=\"tag\"/></p>"
													+"<p>描述<textarea name=\"desc\"></textarea></p>"
													+"<p>设置权限：<br />"
													+"<input type=\"radio\" name=\"privilege\" value=\"公开\" checked/>公开<br />"
													+"<input type=\"radio\" name=\"privilege\" value=\"好友查看\" />好友查看<br />"
													+"<input type=\"radio\" name=\"privilege\" value=\"群组查看\" />群组查看<br /></p>"
													+"<p><input type=\"button\" value=\"保存\" onclick=\"uploadOne()\"/></p>";
	
	document.getElementById("upTags").innerHTML = "<p>批量上传</p>"
												+"标签<input type=\"text\" size=\"30\" />"	
												+"<input type=\"button\" value=\"保存\"/>";
}
//批量上传的标签
/*function uploadtags() {
	var upTagsForm = document.forms["upTagsForm"];
	var tags = upTagsForm['tags'].value;
	var url = 'photo/uploadTags';
	var queryString = "tags=" + tags;
	xmlHttp.onreadystatechange = callbackUptags; //回调函数
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
	xmlHttp.send(queryString);
}

function callbackUptags() {
}*/
//单个上传

function uploadOne() {
	var oneUpForm = document.forms["oneUpForm"];
	var upload_id = oneUpForm['last_id'].value;
	var tag = oneUpForm['tag'].value; 
	var desc = oneUpForm['desc'].value; 
	var url = "oneUpDesc";//note:url 
	var privilege = oneUpForm["privilege"];
	for(var i = 0;i < privilege.length;i++) {
		if(privilege[i].checked == true) {
			 priv = privilege[i].value; 
		}
	} 
	var queryString = "tag=" + tag + "&desc=" + desc + "&privilege=" + priv + "&upload_id=" + upload_id; 
	
	if(tag != "" || desc != "" || priv != "") {
		xmlHttp.open("POST",url,true);
		xmlHttp.onreadystatechange = callbackUpOne; //回调函数
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
		a=xmlHttp.send(queryString);
	}	
	
}
function callbackUpOne() {
	if (xmlHttp.readyState == 4){ 
		if(xmlHttp.status == 200) {
			document.getElementById("oneresult").innerHTML = xmlHttp.responseText;
			//document.getElementById("oneresult").innerHTML = "<p>success</p>";alert('0');
		} else {
			document.getElementById("oneresult").innerHTML = "<p>failed</p>";
		}
	} 
	
}